package com.xinggq.pay.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinggq.common.page.PageCondition;
import com.xinggq.pay.service.dto.YsePayConfigDTO;
import com.xinggq.pay.service.entity.YsePayConfigEntity;
import com.xinggq.pay.service.mapper.YsePayConfigMapper;
import com.xinggq.pay.service.pay.channel.yse.enums.YseMethodEnum;
import com.xinggq.pay.service.service.YsePayConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.xinggq.common.utils.Validate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 银盛支付配置服务实现类
 *
 * @author xinggq
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class YsePayConfigServiceImpl extends
    ServiceImpl<YsePayConfigMapper, YsePayConfigEntity> implements YsePayConfigService {

  private final YsePayConfigMapper ysePayConfigMapper;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void create(YsePayConfigDTO dto) {
    Validate.objectNotNull(dto, "配置信息不能为空");
    this.save(YsePayConfigEntity.fromDTO(dto));
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void update(YsePayConfigDTO dto) {
    Validate.objectNotNull(dto, "配置信息不能为空");
    Validate.objectNotNull(dto.id(), "配置ID不能为空");
    YsePayConfigEntity entity = getById(dto.id());
    Validate.objectNotNull(entity, "配置不存在");
    entity.setMethod(dto.method());
    entity.setMethodExplain(dto.methodExplain());
    entity.setPartnerId(dto.partnerId());
    entity.setSellerId(dto.sellerId());
    entity.setBusinessCode(dto.businessCode());
    entity.setNotifyUrl(dto.notifyUrl());
    this.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void delete(Long id) {
    Validate.objectNotNull(id, "配置ID不能为空");
    removeById(id);
  }

  @Override
  public YsePayConfigDTO getDetail(Long id) {
    Validate.objectNotNull(id, "配置ID不能为空");
    return YsePayConfigDTO.fromEntity(super.getById(id));
  }

  @Override
  public IPage<YsePayConfigDTO> pageQuery(PageCondition<YsePayConfigDTO> queryParam) {

    LambdaQueryWrapper<YsePayConfigEntity> wrapper = new LambdaQueryWrapper<>();
    if (queryParam.getQueryDto() != null && StringUtils.isNotBlank(
        queryParam.getQueryDto().method())) {
      wrapper.like(YsePayConfigEntity::getMethod, queryParam.getQueryDto().method());
    }
    Page<YsePayConfigEntity> objectPage = new Page<>(queryParam.getPageNum(),
        queryParam.getPageSize());
    if (CollectionUtils.isNotEmpty(queryParam.getOrderByColumns())) {
      queryParam.getOrderByColumns().forEach(orderByColumn -> objectPage.addOrder(
          queryParam.getIsDes() ? OrderItem.desc(orderByColumn) : OrderItem.asc(orderByColumn)));
    }
    IPage<YsePayConfigEntity> page = page(objectPage, wrapper);
    return page.convert(YsePayConfigDTO::fromEntity);
  }


  @Override
  public YsePayConfigEntity getConfigByMethod(YseMethodEnum method) {
    return ysePayConfigMapper.selectOne(
        Wrappers.lambdaQuery(YsePayConfigEntity.class)
            .eq(YsePayConfigEntity::getMethod, method.getMethod()));
  }
}